<?php

namespace app\admin\controller;


use app\common\model\Classes;
use app\common\model\ClassesTingke;

class Kebiao extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分页获取班级信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where = array('status' => array('>', 0));
        //是否有查询条件教练id
        if (input('mid')) {
            $where['mid'] = input('mid');
        }
        //是否有查询条件月份
        if (input('month')) {
            $first = input('month') . '-1';//当月的第一天
            $month = input('month');//当月
            $t = date("t", strtotime($first));//最后一天
        } else {
            $first = date('Y-m-01');
            $month = date('Y-m');
            $t = date("t", strtotime($first));
        }
        //空白单元格
        $w = date('w', strtotime($first));
        $w = $w == 0 ? 6 : $w - 1;
        $data = [];
        for ($i = 0; $i < $w; $i++) {
            $data[] = array('list' => [], 'date' => '', 'day' => '');
        }
        //周次班级
        $w_list = [];
        for ($i = 1; $i <= 7; $i++) {
            $where['week'] = array('eq', $i);
            $w_list[$i] = Classes::getAll($where, 'time asc');
        }

        //停课
        $tk_where = array(
            'date' => array('like', $month . '%')
        );
        $tingke = ClassesTingke::getAll($tk_where);


        //日期课表
        for ($i = 1; $i <= $t; $i++) {
            $date = $month . '-' . $i;
            $date = date('Y-m-d',strtotime($date));

            $w = date('w', strtotime($date));
            $w = $w == 0 ? 7 : $w;

            $time = strtotime($date);

            $list = $w_list[$w];
            if ($list) {
                foreach ($list as $k => $v) {
                    //判断班级是否停课
                    if ($tingke) {
                        foreach ($tingke as $tk => $tv) {
                            if ($v['id'] == $tv['cid'] && $tv['date'] == $date) {
                                $list[$k]['status'] = 0;//停课
                            }
                        }
                    }
                    //判断一次班级
                    if (($v['status'] == 2) && (strtotime($date) != strtotime($v['date']))) {
                        unset($list[$k]);
                    }

                    //判断班级的开课时间

                    if(strtotime($v['create_time'])>$time){
                        unset($list[$k]);
                    }

                }
            }
            $data[] = array('list' => $list ? $list : [], 'date' => $i, 'day' => $month . '-' . $i);
        }
        return returnJson(0, '', $data);
    }

    /**
     * 调整课程
     * @return void
     */
    public function tiaoke()
    {
        //表单信息
        $input = input('');

        //停课信息
        if($input['cid']){
            $save = [
                'update_time' => time(),
                'create_time' => time(),
                'cid' => $input['id'],
                'date' => date('Y-m-d',strtotime($input['new_date'])),
            ];
            ClassesTingke::create($save);
        }

        //调课
        if ($input['status'] == 2) {
            $date = $input['new_date'];
            $data = array(
                'name' => $input['name'],
                'status' => 2,
                'week' => date('w', strtotime($date)) == 0 ? 7 : date('w', strtotime($date)),
                'address' => $input['address'],
                'time' => date('H:i', strtotime($date)),
                'mid' => $input['mid'],
                'coach' => $input['coach'],
                'type' => $input['type'],
                'date' => date('Y-m-d', strtotime($date)),
                'update_time' => time(),
                'create_time' => time(),
            );

            $res = Classes::create($data);
            return returnJson(0, '课程调整成功');
        }


        return returnJson(0, '停课成功');
    }
}
